extends Control

const GAME = "res://scene/game/game.tscn"
@onready var click: AudioStreamPlayer = $Click
@onready var mountain: ColorRect = $Mountain
@onready var mouse_effect: Sprite2D = $MouseEffect
@onready var begin: Label = $Begin
var mouse_in_begin:bool=false
@onready var change_skin: Label = $ChangeSkin
var mouse_in_change:bool=false
var mountain_material:ShaderMaterial
var moutain_noise:NoiseTexture2D
var fast_noise:FastNoiseLite
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	if Global.passed_game:
		$ColorFull.sun()
		$Sun.self_modulate=Color("e5b6a24c")
		$Sun/Face.visible=false
	Black.visible=false
	mountain_material=mountain.material
	moutain_noise=mountain_material.get_shader_parameter("noise")
	fast_noise=moutain_noise.noise
	begin.pivot_offset=begin.size/2
	change_skin.pivot_offset=change_skin.size/2
	$Black.visible=false
	$Blur.visible=false
	if Global.passed_game:
		$Black.visible=true
		$Blur.visible=true
		$Blur.material.set_shader_parameter("f",5.0)
		$Black.color=Color(0.0, 0.0, 0.0, 1.0)
		var twn=create_tween()
		twn.tween_property($Blur.material,"shader_parameter/f",0.0,2.0)
		twn.parallel().tween_property($Black,"color",Color(0.0, 0.0, 0.0, 0.0),2.0)
		twn.tween_callback(func():
			$Black.visible=false
			$Blur.visible=false
			)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	fast_noise.offset.x+=delta*10
	if mouse_in_begin:begin.scale=begin.scale.move_toward(Vector2(1.2,1.2),delta)
	else:begin.scale=begin.scale.move_toward(Vector2.ONE,delta)
	if mouse_in_change:change_skin.scale=change_skin.scale.move_toward(Vector2(1.2,1.2),delta)
	else:change_skin.scale=change_skin.scale.move_toward(Vector2.ONE,delta)
func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.button_index==MOUSE_BUTTON_LEFT:
			if event.pressed:
				$MouseEffect.play(event.position)
				click.play()
			else:
				pass


func _on_begin_mouse_entered() -> void:
	mouse_in_begin=true


func _on_begin_mouse_exited() -> void:
	mouse_in_begin=false


func _on_change_skin_mouse_entered() -> void:
	mouse_in_change=true


func _on_change_skin_mouse_exited() -> void:
	mouse_in_change=false



func _on_high_light_timeout() -> void:
	begin.material.set_shader_parameter("time",-0.5)
	var twn:=create_tween()
	twn.tween_property(begin.material,"shader_parameter/time",0.7,1.0)


func _on_button_pressed() -> void:
	Global.enter_said_should_restart=true
	if not ResourceLoader.load_threaded_get_status(GAME)==ResourceLoader.THREAD_LOAD_LOADED:
		ResourceLoader.load_threaded_request(GAME)
	if FileAccess.file_exists(Global.save_path):
		if not ResourceLoader.load_threaded_get_status(Global.save_path)==ResourceLoader.THREAD_LOAD_LOADED:
			ResourceLoader.load_threaded_request(Global.save_path)
	$Black.color=Color(0.0, 0.0, 0.0, 0.0)
	$Blur.material.set_shader_parameter("f",0.0)
	$Black.visible=true
	$Blur.visible=true
	var twn=create_tween()
	twn.tween_property($Blur.material,"shader_parameter/f",5.0,2.0)
	twn.parallel().tween_property($Black,"color",Color(0.0, 0.0, 0.0, 1.0),2.0)
	twn.tween_callback(func():
		Black.visible=true
		var load_status:=ResourceLoader.load_threaded_get_status(GAME)
		print(load_status)
		if load_status==ResourceLoader.THREAD_LOAD_LOADED:
			var packed_next_scene:=ResourceLoader.load_threaded_get(GAME)
			get_tree().change_scene_to_packed(packed_next_scene)
		else:
			var _twn:=create_tween()
			_twn.tween_property($Blur.material,"shader_parameter/f",0.0,2.0)
			_twn.parallel().tween_property($Black,"color",Color(0.0, 0.0, 0.0, 0.0),2.0)
			_twn.tween_callback(func():
				$Black.visible=false
				$Blur.visible=false
				)
		)


func _on_continue_game_pressed() -> void:
	Global.enter_said_should_restart=false
	if not ResourceLoader.load_threaded_get_status(GAME)==ResourceLoader.THREAD_LOAD_LOADED:
		if not ResourceLoader.load_threaded_get_status(GAME)==ResourceLoader.THREAD_LOAD_IN_PROGRESS:
			ResourceLoader.load_threaded_request(GAME)
	$Black.color=Color(0.0, 0.0, 0.0, 0.0)
	$Blur.material.set_shader_parameter("f",0.0)
	$Black.visible=true
	$Blur.visible=true
	var twn=create_tween()
	twn.tween_property($Blur.material,"shader_parameter/f",5.0,2.0)
	twn.parallel().tween_property($Black,"color",Color(0.0, 0.0, 0.0, 1.0),2.0)
	twn.tween_callback(func():
		Black.visible=true
		var load_status:=ResourceLoader.load_threaded_get_status(GAME)
		print(load_status)
		if load_status==ResourceLoader.THREAD_LOAD_LOADED:
			var packed_next_scene:=ResourceLoader.load_threaded_get(GAME)
			get_tree().change_scene_to_packed(packed_next_scene)
		else:
			var _twn:=create_tween()
			_twn.tween_property($Blur.material,"shader_parameter/f",0.0,2.0)
			_twn.parallel().tween_property($Black,"color",Color(0.0, 0.0, 0.0, 0.0),2.0)
			_twn.tween_callback(func():
				$Black.visible=false
				$Blur.visible=false
				)
		)

func _enter_tree() -> void:
	if get_tree().paused==true:get_tree().paused=false
func _exit_tree() -> void:
	if not Global.enter:Global.enter=PackedScene.new()
	Global.enter.pack(self)
